#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env python
#
# Autogenerated by Thrift Compiler (0.9.2)
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
#  options string: py:utf8strings
#

import sys
import pprint
from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import TSSLSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol

from storm import Nimbus
from storm.ttypes import *

if len(sys.argv) <= 1 or sys.argv[1] == '--help':
  print('')
  print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] [-s[sl]] function [arg1 [arg2...]]')
  print('')
  print('Functions:')
  print('  void submitTopology(string name, string uploadedJarLocation, string jsonConf, StormTopology topology)')
  print('  void submitTopologyWithOpts(string name, string uploadedJarLocation, string jsonConf, StormTopology topology, SubmitOptions options)')
  print('  void killTopology(string name)')
  print('  void killTopologyWithOpts(string name, KillOptions options)')
  print('  void activate(string name)')
  print('  void deactivate(string name)')
  print('  void rebalance(string name, RebalanceOptions options)')
  print('  void uploadNewCredentials(string name, Credentials creds)')
  print('  string beginFileUpload()')
  print('  void uploadChunk(string location, string chunk)')
  print('  void finishFileUpload(string location)')
  print('  string beginFileDownload(string file)')
  print('  string downloadChunk(string id)')
  print('  string getNimbusConf()')
  print('  ClusterSummary getClusterInfo()')
  print('  TopologyInfo getTopologyInfo(string id)')
  print('  TopologyInfo getTopologyInfoWithOpts(string id, GetInfoOptions options)')
  print('  string getTopologyConf(string id)')
  print('  StormTopology getTopology(string id)')
  print('  StormTopology getUserTopology(string id)')
  print('')
  sys.exit(0)

pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
uri = ''
framed = False
ssl = False
http = False
argi = 1

if sys.argv[argi] == '-h':
  parts = sys.argv[argi+1].split(':')
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  argi += 2

if sys.argv[argi] == '-u':
  url = urlparse(sys.argv[argi+1])
  parts = url[1].split(':')
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  else:
    port = 80
  uri = url[2]
  if url[4]:
    uri += '?%s' % url[4]
  http = True
  argi += 2

if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
  framed = True
  argi += 1

if sys.argv[argi] == '-s' or sys.argv[argi] == '-ssl':
  ssl = True
  argi += 1

cmd = sys.argv[argi]
args = sys.argv[argi+1:]

if http:
  transport = THttpClient.THttpClient(host, port, uri)
else:
  socket = TSSLSocket.TSSLSocket(host, port, validate=False) if ssl else TSocket.TSocket(host, port)
  if framed:
    transport = TTransport.TFramedTransport(socket)
  else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Nimbus.Client(protocol)
transport.open()

if cmd == 'submitTopology':
  if len(args) != 4:
    print('submitTopology requires 4 args')
    sys.exit(1)
  pp.pprint(client.submitTopology(args[0],args[1],args[2],eval(args[3]),))

elif cmd == 'submitTopologyWithOpts':
  if len(args) != 5:
    print('submitTopologyWithOpts requires 5 args')
    sys.exit(1)
  pp.pprint(client.submitTopologyWithOpts(args[0],args[1],args[2],eval(args[3]),eval(args[4]),))

elif cmd == 'killTopology':
  if len(args) != 1:
    print('killTopology requires 1 args')
    sys.exit(1)
  pp.pprint(client.killTopology(args[0],))

elif cmd == 'killTopologyWithOpts':
  if len(args) != 2:
    print('killTopologyWithOpts requires 2 args')
    sys.exit(1)
  pp.pprint(client.killTopologyWithOpts(args[0],eval(args[1]),))

elif cmd == 'activate':
  if len(args) != 1:
    print('activate requires 1 args')
    sys.exit(1)
  pp.pprint(client.activate(args[0],))

elif cmd == 'deactivate':
  if len(args) != 1:
    print('deactivate requires 1 args')
    sys.exit(1)
  pp.pprint(client.deactivate(args[0],))

elif cmd == 'rebalance':
  if len(args) != 2:
    print('rebalance requires 2 args')
    sys.exit(1)
  pp.pprint(client.rebalance(args[0],eval(args[1]),))

elif cmd == 'uploadNewCredentials':
  if len(args) != 2:
    print('uploadNewCredentials requires 2 args')
    sys.exit(1)
  pp.pprint(client.uploadNewCredentials(args[0],eval(args[1]),))

elif cmd == 'beginFileUpload':
  if len(args) != 0:
    print('beginFileUpload requires 0 args')
    sys.exit(1)
  pp.pprint(client.beginFileUpload())

elif cmd == 'uploadChunk':
  if len(args) != 2:
    print('uploadChunk requires 2 args')
    sys.exit(1)
  pp.pprint(client.uploadChunk(args[0],args[1],))

elif cmd == 'finishFileUpload':
  if len(args) != 1:
    print('finishFileUpload requires 1 args')
    sys.exit(1)
  pp.pprint(client.finishFileUpload(args[0],))

elif cmd == 'beginFileDownload':
  if len(args) != 1:
    print('beginFileDownload requires 1 args')
    sys.exit(1)
  pp.pprint(client.beginFileDownload(args[0],))

elif cmd == 'downloadChunk':
  if len(args) != 1:
    print('downloadChunk requires 1 args')
    sys.exit(1)
  pp.pprint(client.downloadChunk(args[0],))

elif cmd == 'getNimbusConf':
  if len(args) != 0:
    print('getNimbusConf requires 0 args')
    sys.exit(1)
  pp.pprint(client.getNimbusConf())

elif cmd == 'getClusterInfo':
  if len(args) != 0:
    print('getClusterInfo requires 0 args')
    sys.exit(1)
  pp.pprint(client.getClusterInfo())

elif cmd == 'getTopologyInfo':
  if len(args) != 1:
    print('getTopologyInfo requires 1 args')
    sys.exit(1)
  pp.pprint(client.getTopologyInfo(args[0],))

elif cmd == 'getTopologyInfoWithOpts':
  if len(args) != 2:
    print('getTopologyInfoWithOpts requires 2 args')
    sys.exit(1)
  pp.pprint(client.getTopologyInfoWithOpts(args[0],eval(args[1]),))

elif cmd == 'getTopologyConf':
  if len(args) != 1:
    print('getTopologyConf requires 1 args')
    sys.exit(1)
  pp.pprint(client.getTopologyConf(args[0],))

elif cmd == 'getTopology':
  if len(args) != 1:
    print('getTopology requires 1 args')
    sys.exit(1)
  pp.pprint(client.getTopology(args[0],))

elif cmd == 'getUserTopology':
  if len(args) != 1:
    print('getUserTopology requires 1 args')
    sys.exit(1)
  pp.pprint(client.getUserTopology(args[0],))

else:
  print('Unrecognized method %s' % cmd)
  sys.exit(1)

transport.close()
